package com.hnust.service.impl;

import com.hnust.dao.*;
import com.hnust.domain.*;
import com.hnust.domain.Class;
import com.hnust.domain.record.CourseGroupInfo;
import com.hnust.domain.record.EditCourseGroupInfoRecord;
import com.hnust.service.ImportService;
import com.hnust.util.IDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 创建人：游磊
 * 创建时间：2020/10/7
 * 描述：和导入有关的操作
 */
@Service("ImportService")
public class ImportServiceImpl implements ImportService {

    @Autowired
    private CourseGroupDao courseGroupDao;

    @Autowired
    private TeacherDao teacherDao;

    @Autowired
    private ClassDao classDao;

    @Autowired
    private TeaCourseGroupDao teaCourseGroupDao;

    @Autowired
    private ClassCourseGroupDao classCourseGroupDao;
    // 获取课程组信息
    @Override
    public EditCourseGroupInfoRecord GetCourseGroupInfo(String colId) throws Exception {
        // 初始化课程组
        EditCourseGroupInfoRecord edit = new EditCourseGroupInfoRecord();
        // 先查询所有的学院
        List<HashMap<String,String>> courses = new ArrayList<>();
        List<CourseGroup>  course = courseGroupDao.getCourseGroupListByColId(colId);
        for (CourseGroup course1: course){
            courses.add(new HashMap<String,String>(){{
                put("name",course1.getName());
                put("id",course1.getCouId());
            }});
        }
//        System.out.println(courses);
        edit.setCourseGroups(courses);
        // 查询所有的开课时间
        List<String> open = courseGroupDao.getCourseGroupOpenTime();
        edit.setCourseGroupTimes(open);
        // 获取所有的老师信息
        List<Teacher> teachers = teacherDao.findTeacherBySearch(colId,null);
        List<HashMap<String,String>> teacher = new ArrayList<>();
        for (Teacher teacher1: teachers){
            teacher.add(new HashMap<String,String>(){{
                put("name",teacher1.getName());
                put("id",teacher1.getTeaId());
            }});
        }
        edit.setCourseGroupTeacher(teacher);
        edit.setCourseGroupLeader(teacher);
        // 获取所有的班级信息
        List<Class> classes = classDao.findClassByCollegeId(colId);
        List<HashMap<String,String>> Class = new ArrayList<>();
        for (Class class1: classes){
            Class.add(new HashMap<String,String>(){{
                put("id",class1.getClaId());
                put("name", class1.getGrade() + class1.getClaNumber());
            }});
        }
        edit.setCourseGroupClass(Class);

        return edit;
    }

    // 批量导入课程组信息
    @Override
    public Boolean BatchImportCourseGroupInfo(List<CourseGroupInfo> courseGroupInfos) throws Exception {
//        for (CourseGroupInfo courseGroupInfo: courseGroupInfos){
//            // 先手动添加课程组 (这里需要等张汝往的接口写完)
////        courseGroupDao.addCourseGroup(courseGroupInfo);
//            // 自动添加老师信息
//            for (Map<String,String> k:courseGroupInfo.getSimpleCourseGroupInfo().getTeachers()){
//                // 添加老师课程课程组信息
//                TeaCourseGroup teaCourseGroup = new TeaCourseGroup();
//                teaCourseGroup.setId(IDUtil.getPrimaryKey());
//                teaCourseGroup.setGroupLeader(2);
//                teaCourseGroup.setTeaId(k.get("id"));
//                teaCourseGroup.setCouGroId(courseGroupInfo.getSimpleCourseGroupInfo().getCourseGroupId());
//                teaCourseGroupDao.addTeaCourseGroup(teaCourseGroup);
//            }
//            // 自动添加班级课程组
//            for (Map<String,String> k:courseGroupInfo.getSimpleCourseGroupInfo().getClasses()){
//                // 添加老师课程课程组信息
//                ClassCourseGroup classCourseGroup= new ClassCourseGroup();
//                classCourseGroup.setId(IDUtil.getPrimaryKey());
//                classCourseGroup.setClaId(k.get("id"));
//                classCourseGroup.setCouGroId(courseGroupInfo.getSimpleCourseGroupInfo().getCourseGroupId());
//                classCourseGroupDao.addClassCourseGroup(classCourseGroup);
//            }
//        }
        return true;
    }
}
